define(function(require, exports, module) {

var $=require("jq.js");
var Dialog=require("/pinzhiV2.0/js/pz_use_dialog.js");

// 触发登录事件
var pointBtn = $('#deliver_goods');

var selectTypeBtn = $("#selectTypeBtn")
// 写入已发货html

pointBtn.on("click",function(){
	//innerDeliverFrom();------------------------------------------------------
	popWindow($(this));
});

//写入退款HTML
selectTypeBtn.on("click",function(){
	var selectType = "<div class='J_PZLogin'id='select_type'><div class='J_PZLogin_box'id='validate-login'><div class='J_PZLogin_caption'><span class='J_PZLogin_caption_text'><a href='javascript:'class='close'>X</a>请选择</span></div><div class='J_PZLogin_content'><div class='select_type'><h3>您提交的售后订单，涉及到赠送或消费积分，请选择当您的积分余额不足时，退款处理方法：</h3><p class='select'><select id='choose_select'><option value='1'>请等我转回积分后退款</option><option value='2'>积分不足时，用退款额进行抵销</option></select></p><p><a href='javascript:' id='selectOk'>确认</a></p><h4>说明</h4><p>1、”请等我转回积分后退款“；当您积分余额不足时，品质365将等您补齐积分后，在为您办理退款。</p><p>2、”积分不足时，用退款额进行抵销“；当您积分余额不足时，我们将直接用退款金额抵销您不足的积分。</p></div></div></div></div>"
	innerDeliverFrom(selectType);

});





var center = function(dom){

	 	var top = ($(window).height() - dom.height())/2;
         var left = ($(window).width() - dom.width())/2;
         var scrollTop = $(document).scrollTop();
         var scrollLeft = $(document).scrollLeft();


         if (top<0) {
          var top = 30;
         }
         dom.css( { position : 'absolute', 'top' : top+scrollTop, left : left + scrollLeft } );
}



//写入已发货html
var innerDeliverFrom = function(){
	
//	修改快递信息数据
	var cys = $("#cys1").val().trim(),
		ydh = $("#ydh1").val().trim(),
		imgSrc = $("#img11").val();
	
	
	$("#cys").val(cys);	
	$("#ydh").val(ydh);		
	$("#img1").attr("src",imgSrc);		
			
	
	var mask = "<div class='J_PZLogin_mask'></div>"
	$("#deliverGoods").show();
	$("body").append(mask);
	var J_PZLogin = $(".J_PZLogin");
	var css = require('../css/pz_login.css'); //引入css文件
	$('.J_PZLogin_mask').css("height",$(window).height());
	center(J_PZLogin);

	var deliverGoodsOK = function(){
		var cys = $("#cys").val().trim();
		var ydh = $("#ydh").val().trim();
		var docId = $("#docId").val();
		var path = $("#img1").attr("src");
		
		if(cys!='' && ydh!='' && path!=''){
			var data={cys:cys,ydh:ydh,docId:docId,path:path};
			data = JSON.stringify(data);
			$.get("/erp/customerDocument/updateCustomerDocumentDeliveryInfo.do",{data:data},function(json){
				if(json.success){
					closeLayer();
					window.location.reload();
				}else{
					new Dialog({
						type:"confirm",
						  text:"失败",
						  tipsText:json.msg||"承运商和运单号必须填写准确",
						  buttonSureText:"知道了", 
						  buttonCancelText:"关闭", 
						  mask:0.3	
					});
				}
			},"json");
		}else{
			new Dialog({
				type:"confirm",
				  text:"失败",
				  tipsText:"必须都要填写",
				  buttonSureText:"知道了", 
				  buttonCancelText:"关闭", 
				  mask:0.3	
			});
		}
		
	}

	
	//获取已收货按钮
	var deliver_goodsOK = $("#deliver_goodsOK");
	deliver_goodsOK.on("click",function(){

		//deliverGoodsOK();
		
		
		var d = new new Dialog({
							  type:"alert",
							  text:"定位",
							  tipsText:"必须都要填写",
							  buttonSureText:"知道了", 
							  buttonCancelText:"关闭", 
							  mask:0.3	
						});
		

	});

	//获取退款按钮
	var selectOk = $("#selectOk");
	selectOk.on("click",function(){

		selectOkFun();

	});


	$(".close").on("click",function(){

		closeLayer();
	});

}


// 确认已发货
var deliverGoodsOK = function(){

	var cys = $("input[name='cys']").val();
	var ydh = $("input[name='ydh']").val();



	closeLayer();


}


//确认退款

var selectOkFun = function(){

	var  choose_select = $("#choose_select").val();

	closeLayer();

};


//关闭弹出层
var closeLayer = function(){
	$(".J_PZLogin").hide();
	$('.J_PZLogin_mask').remove();


};

//添加e钱包支付供应商运费js
(function(){
	var btn = $("#pay_partner_delivery");
	var money = btn.attr("data-deliveryFee");
	if(btn.length>0){
		btn.click(function(){
			$.post("/thridPartner/customerManager/getUserEmoney.do",{},function(json){
				if(json.success){
					var emoney = json.emoney;
					if(emoney>=money){
						var html = "用户E钱包余额:"+emoney+"<br>";
						html+="需支付给供应商的运费:<strong style='color:red;'>"+money+"</strong>元";
						new Dialog({
							type : "confirm",
							text : "提示",
							tipsText : html,
							height : 130,
							buttonSureText : "支付",
							buttonCancelText : "取消",
							mask : 0.3,
							callBack : function(){
								var docId = btn.attr("data-docId");
								$.post("/vipCenter/payPartnerDeliveryFee.do",{docId : docId},function(json){
									if(json.success){
										alert(json.msg||"运费支付成功");
										window.location.reload();
									}else{
										alert(json.msg||"运费支付失败");
									}
								},"json");
							}
						});
					}else{
						var html = "用户E钱包余额:"+emoney+"<br>";
						html+="需支付给供应商的运费:<strong style='color:red;'>"+money+"</strong>元<br>";
						html+="<strong style='color:red;'>E钱包余额不足</strong>";
						new Dialog({
							type : "confirm",
							text : "提示",
							height : 150,
							tipsText : html,
							buttonSureText : "充值E钱包",
							buttonCancelText : "取消",
							mask : 0.3,
							callBack : function(){
								window.location = "/pinzhiV2.0/mCenter/recharge/depositRecharge.jsp";
							}
						});
					}
				}
			},"json");			
		});
	}
})();

(function(){
	var cancelBtn = $("#cust-cancel");
	if(cancelBtn.length>0){
		cancelBtn.click(function(){
			var custId=$(this).attr("data-custId");
			var html = "<div>取消原因:<textarea id='cancelReasonInput'></textarea></div>"
			new Dialog({
				type : "confirm",
				text : "提示",
				height : 130,
				tipsText : html,
				buttonSureText : "确认取消",
				buttonCancelText : "关闭",
				mask : 0.3,
				callBack : function(){
					var reason = $("#cancelReasonInput").val()||"";
					var params = {
						custId : custId,
						isFrontUser : 1,
						reason : reason
					};
					$.post("/thirdPartner/customerManager/cancelCustomerDocument.do",params,function(json){
						if(json.success){
							alert(json.msg||"取消成功");
							window.location.reload();
						}else{
							alert(json.msg||"取消失败");
						}
					},"json");
				}
			});
		});
	}
})();

//客服申述
(function(){
	$("#cust-leaveMessage").click(function(){
		var custId=$(this).attr("data-custId");
		var html = [
		            "<div id='cust-message-win'>",
			            "您的姓名:<input name='linkMan' >",
			            "您的联系方式:<input name='linkPhone'>",
			            "您的留言:<textarea name='message' style='display:block;width:93%;resize:none;'></textarea>",
		            "</div>"
		            ].join("</br>")
		new Dialog({
			type : "confirm",
			text : "提示",
			height : 300,
			tipsText : html,
			buttonSureText : "确认留言",
			buttonCancelText : "关闭",
			mask : 0.3,
			callBack : function(){
				var linkMan = $("#cust-message-win input[name=linkMan]").val()||"";
				var linkPhone = $("#cust-message-win input[name=linkPhone]").val()||"";
				var message = $("#cust-message-win textarea[name=message]").val()||"";
				if(!linkMan){
					alert("联系人的姓名不能为空");
					return ;
				}
				if(!/^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/.test(linkPhone)){
					alert("联系电话的格式不正确");
					return ;
				}
				if(!message){
					alert("留言不能为空");
					return ;
				}
				if(message.length>200){
					alert("留言不能超过200个字");
					return ;
				}
				
				var params = {
					linkMan : linkMan,
					linkPhone : linkPhone,
					message : message,
					custId : custId
				};
				$.post("/thirdPartner/customerManager/leaveMessage.do",params,function(json){
					if(json.success){
						alert(json.msg||"留言成功");
						window.location.reload();
					}else{
						alert(json.msg||"留言失败");
					}
				},"json");
			}
		});
	});
})();

var popWindowHtml = [
            "<div class='form_show_s'>",
            '<dl>',
                '<dt>',
                    '快递公司',
                '</dt>',
                '<dd>',
                    "<input type='text' style='width:180px;' name='cys' id='cys'>",
                '</dd>',
            '</dl>',
            '<dl>',
                '<dt>',
                    '运单号',
                '</dt>',
                '<dd>',
                    "<input type='text' style='width:180px;' name='ydh' id='ydh'>",
                '</dd>',
            '</dl>',
            '运单照片',
			'<div class="upload-wrapper" data-btn-show="show">',
				'<ul class="clearfix">',
					'<li><img id="img1" width="60" height="60"> <span',
						'class="btn-spm"> <a href="#uploadimag"',
							'class="upload-btn" data-series="img1">上传</a><a href="#delimg"',
							'class="del-btn" data-series="img1">删除</a>',
					'</span></li>',
				'</ul>',
			'</div>',
			'<dl id="deliveryAmonutView" style="display:none;">',
				'<dt>',
				    '运费金额',
				'</dt>',
				'<dd>',
				    "<input type='text' style='width:180px;' name='deliveryAmonut' id='deliveryAmonut'>",
				'</dd>',
			'</dl>',
        '</div>'
            ].join("\n");


var win = null;

function popWindow(clickBtn){
	var canUpdate = clickBtn.attr("data-canUpdateDelivery");
	if(!win){
		win = new Dialog({
				type : "confirm",
				text : "提示",
				height : 320,
				width : 200,
				tipsText : popWindowHtml,
				buttonSureText : "确认已发货",
				buttonCancelText : "关闭",
				mask : 0.3,
				callBack:function(){
						var cys = $("#cys").val(),
							ydh = $("#ydh").val(),
							src = $("#img1").attr("src"),
							docId = $("#docId").val(),
							path = $("#img1").attr("src"),
							deliveryAmount = $("#deliveryAmonut").val();
							if(canUpdate=="1"&&!/^\d+(?:.\d+)?$/.test((deliveryAmount||"")+"")){
								alert("运费金额需要是大于0的数字");
								return true;
							}
						if(cys&&ydh&&src){
							var data={cys:cys,ydh:ydh,docId:docId,path:path,docId:docId,deliveryAmount:deliveryAmount,canUpdate:canUpdate};
							data = JSON.stringify(data);
							$.get("/erp/customerDocument/updateCustomerDocumentDeliveryInfo.do",{data:data},function(json){
								if(json.success){
									window.location.reload();
								}else{
									alert(json.msg||"承运商和运单号必须填写准确");
									return true;
								}
							},"json");
						}else{
							alert("请输入完整");
							return true;
						};
						this.hideWin();
						return true;
					},
				cancelCallBack : function(){
					this.hideWin();
					return true;
				}
				});
			win.showWin = function(){
				this.mask.show();
				this.J_PZDialog.show();
			};
			win.hideWin = function(){
				this.mask.hide();
				this.J_PZDialog.hide();
			};
			seajs.use("/pinzhiV2.0/js/vipCenter/upload-image.js");
	}else{
		win.showWin();
	}
	var cys = $("#cys1").val().trim(),
	ydh = $("#ydh1").val().trim(),
	imgSrc = $("#img11").val();
	$("#cys").val(cys);	
	$("#ydh").val(ydh);		
	$("#img1").attr("src",imgSrc);	
	
	if(canUpdate=="1"){
		$("#deliveryAmonutView").show();
	}else{
		$("#deliveryAmonutView").hide();
	}
};

});
